package v1

import (
	"RoccBlog/api"
	"RoccBlog/global"
	"RoccBlog/model"
	"RoccBlog/model/req_resp"
	v1 "RoccBlog/service/v1"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type RoleController struct {
	api.BaseController
	api.BaseOperationController
	Service           *v1.RoleService
	PermissionService *v1.PermissionService
	RouterService     *v1.RouterService
}

func (c *RoleController) RegisterController(engine *gin.RouterGroup) {
	// 配置server
	c.Service = global.GetService(v1.RoleService{}).(*v1.RoleService)
	c.PermissionService = global.GetService(v1.PermissionService{}).(*v1.PermissionService)
	c.RouterService = global.GetService(v1.RouterService{}).(*v1.RouterService)

	g := engine.Group("/role")
	{
		g.POST("", c.Add)
		g.DELETE("", c.Remove)
		g.PUT("", c.Modify)
		g.GET("/list", c.Find)
		g.GET("", c.FindById)
		g.PUT("/permission", c.ModifyPermission)
		g.PUT("/router", c.ModifyRouter)
	}
}

func init() {
	global.RegisterController(&RoleController{})
}

// Add
// @Tags 角色管理
// @Summary 创建角色
// @Produce  json
// @Param role body model.Role required "用户"
// @Success 200 {object} api.Result "成功"
// @Router /role [post]
func (c *RoleController) Add(ctx *gin.Context) {
	var role model.Role
	err := ctx.ShouldBind(&role)
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}

	err = c.Service.Add(ctx, role)

	c.HandleResponseNoResult(ctx, err, "创建角色失败", "创建角色成功")
}

// Remove
// @Tags 角色管理
// @Summary 删除角色
// @Produce  json
// @Param id query int required "角色ID"
// @Success 200 {object} api.Result "成功"
// @Router /role [delete]
func (c *RoleController) Remove(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Query("id"))
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}

	err = c.Service.Remove(ctx, id)

	c.HandleResponseNoResult(ctx, err, "删除角色失败", "删除角色成功")
}

// ModifyPermission 修改角色权限
// @Tags 角色管理
// @Summary 修改角色权限
// @Produce  json
// @Param role body req_resp.ModifyRolePermission required "角色权限"
// @Success 200 {object} api.Result "成功"
// @Router /role/permission [put]
func (c *RoleController) ModifyPermission(ctx *gin.Context) {

	var rolePermission req_resp.ModifyRolePermission
	err := ctx.ShouldBind(&rolePermission)
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}

	//查找权限的值
	list, err := c.PermissionService.FindByIds(rolePermission.Permissions)
	if err != nil {
		c.Response(ctx, http.StatusInternalServerError, "查询权限失败！"+err.Error(), nil)
		return
	}
	//过滤掉分组内容
	filter := make([]model.Permission, 0)
	for _, val := range list {
		if val.ParentId != 0 {
			filter = append(filter, val)
		}
	}

	//查找到角色
	role, err := c.Service.FindById(ctx, rolePermission.RoleId)
	if err != nil {
		c.Response(ctx, http.StatusInternalServerError, "查询角色失败！"+err.Error(), nil)
		return
	}
	role.Permissions = filter
	err = c.Service.ModifyPermission(ctx, role)

	//返回值
	c.HandleResponseNoResult(ctx, err, "修改角色权限失败", "修改角色权限成功")
}

// ModifyRouter 修改角色路由
// @Tags 角色管理
// @Summary 修改角色路由
// @Produce  json
// @Param role body req_resp.ModifyRoleRouter required "角色权限"
// @Success 200 {object} api.Result "成功"
// @Router /role/router [put]
func (c *RoleController) ModifyRouter(ctx *gin.Context) {
	var roleRouter req_resp.ModifyRoleRouter
	err := ctx.ShouldBind(&roleRouter)
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}

	// 查找路由列表
	list, err := c.RouterService.FindByIds(roleRouter.Routers)
	if err != nil {
		c.Response(ctx, http.StatusInternalServerError, "查询路由失败！"+err.Error(), nil)
		return
	}

	// 查询角色
	role, err := c.Service.FindById(ctx, roleRouter.RoleId)
	if err != nil {
		c.Response(ctx, http.StatusInternalServerError, "查询角色失败！"+err.Error(), nil)
		return
	}

	role.Routers = list

	c.Service.ModifyRouter(role)

	c.HandleResponseNoResult(ctx, err, "修改角色路由失败", "修改角色路由成功")
}

// Modify
// @Tags 角色管理
// @Summary 修改角色
// @Produce  json
// @Param role body model.Role required "角色"
// @Success 200 {object} api.Result "成功"
// @Router /role [put]
func (c *RoleController) Modify(ctx *gin.Context) {
	var role model.Role

	err := ctx.ShouldBind(&role)
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}

	err = c.Service.Modify(ctx, role)
	c.HandleResponseNoResult(ctx, err, "修改角色失败", "修改角色成功")
}

// Find
// @Tags 角色管理
// @Summary 查找角色列表
// @Produce  json
// @Param pageNum query int false "页码"
// @Param pageSize query int false "每页数量"
// @Success 200 {object} api.Result "成功"
// @Router /role/list [get]
func (c *RoleController) Find(ctx *gin.Context) {
	pageInfo, err := c.Service.Find(ctx)
	c.HandleResponseResult(ctx, err, "查询角色列表失败", pageInfo)
}

// FindById
// @Tags 角色管理
// @Summary 查找角色by id
// @Produce  json
// @Param id query int required "角色ID"
// @Success 200 {object} api.Result "成功"
// @Router /role [get]
func (c *RoleController) FindById(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Query("id"))

	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}

	role, err := c.Service.FindById(ctx, id)
	c.HandleResponseResult(ctx, err, "查询角色失败", role)
}
